package com.daguchuangyi.erp_android.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.daguchuangyi.erp_android.R;
import com.jude.utils.JUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

import it.sephiroth.android.library.exif2.ExifInterface;
import it.sephiroth.android.library.exif2.ExifTag;

import static com.yalantis.ucrop.util.BitmapLoadUtils.calculateInSampleSize;

/**
 * Created by lijianjian on 2018/3/12.
 */

public class ImageUtil {
    public static RequestOptions getGlideRequestOptions() {
        return new RequestOptions().placeholder(R.drawable.product_default);
    }

    public static RequestOptions getProductSummaryOptions() {
        return new RequestOptions().placeholder(R.drawable.product_default)
                .transform(new RoundedCorners(JUtils.dip2px(3)));
    }

    public static RequestOptions getAvatarRequestOptions() {
        return new RequestOptions()
                .circleCrop()
                .placeholder(R.drawable.user_default_avatar)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.user_default_avatar);
    }


    // 根据路径获得图片并压缩，返回bitmap用于显示
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 480, 800);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

    //把bitmap转换成Base64
    public static String bitmapToString(String filePath) {
        Bitmap bm = getSmallBitmap(filePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        bm.compress(Bitmap.CompressFormat.JPEG, 75, baos);
        byte[] b = baos.toByteArray();
        JUtils.Log("压缩后的大小=" + b.length);
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    public static void saveExif(String oldFilePath, String newFilePath) throws Exception {
        ExifInterface exif = new ExifInterface();
        exif.readExif(oldFilePath, ExifInterface.Options.OPTION_ALL );

        List<ExifTag> all_tags = exif.getAllTags();
        for (ExifTag tag : all_tags) {
            JUtils.Log("jianjian","tag : "+tag.toString());
        }
        exif.writeExif(newFilePath);
    }

}
